#!/usr/bin/env python
# coding=utf-8

import kaldi_io
import numpy as np
import os
import argparse


def split_dvectors(scp_file_path, out_dir):
	for idx, (key,mat) in enumerate(kaldi_io.read_mat_scp(scp_file_path)):
		if '_' in key:
			key = key.split('_')[0]
		dst_path = os.path.join(out_dir, key)
		if not os.path.exists(dst_path):
			os.makedirs(dst_path)

		dst_path = os.path.join(dst_path, "dvector.npy")
		print("{} {}: {}".format(idx+1, key, mat.shape))
		np.save(dst_path, mat)


def load_dvectors(scp_file_path):
	for idx, (key,mat) in enumerate(kaldi_io.read_mat_scp(scp_file_path)):
		if idx == 0:
			dvectors = mat
		else:
			dvectors = np.vstack((dvectors, mat))
	return dvectors


if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	parser.add_argument("--scp-file-path", type=str, default="dvector.scp", help="scp file path")
	parser.add_argument("--out-dir", type=str,default="out/" ,help="numpy out dir")
	args = parser.parse_args()
	split_dvectors(args.scp_file_path, args.out_dir)

